package org.txazo.blog.commons.parse;

import java.io.*;

public class HtmlParser {

	public static int FLAG = 0; // 字符串标记
	public static int TAG = 0; // 标签标记
	public static int DOCT = 0; // HTML文档声明标记
	public static int COMM = 0; // 注释标记

	public static void main(String[] args) {
		readHtml(new File(JavaParser.class.getResource("").getPath()
				.replaceFirst("bin", "src")
				+ "source/parse.html"));
	}

	public static void readHtml(File file) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(
					file), "UTF-8"));
			String temp = null;
			System.out.print("<span class=\"xml-pln\">");
			while ((temp = br.readLine()) != null) {
				parseHtml(temp);
			}
			System.out.print("</span>");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void parseHtml(String line) {
		if (line.equals("")) {
			// 空行
		} else {
			// 非空行
			// 补全空格
			int i = 0;
			int length = line.length();
			for (; i < length; i++) {
				if (!(line.charAt(i) == 9)) {
					break;
				}
			}
			i *= 4;
			for (int j = 0; j < i; j++) {
				System.out.print(" ");
			}
			// 去掉前后空格
			String trim = line.trim();
			// HTML文档声明
			if (trim.startsWith("<!DOCTYPE")) {
				if (trim.endsWith(">")) {
					System.out.print("<span class=\"xml-doc\">&lt;"
							+ trim.substring(1, trim.length() - 1) + "&gt;");
				} else {
					System.out.print("<span class=\"xml-doc\">&lt;"
							+ trim.substring(1, trim.length()));
					DOCT = 1;
				}
			} else if (DOCT == 1) {
				System.out.print("    ");
				if (trim.endsWith(">")) {
					System.out.print(trim.substring(0, trim.length() - 1)
							+ "&gt;</span>");
					DOCT = 0;
				} else {
					System.out.print(trim);
				}
			} else {
				for (int k = 0; k < trim.length(); k++) {
					char c = trim.charAt(k);
					// 字符串
					if (FLAG == 1) {
						if (c == '"') {
							// 右双引号
							FLAG = 0;
							System.out.print(c + "</span>");
						} else {
							System.out.print(c);
						}
						continue;
					}
					// 行首属性
					if (TAG == 1 && k == 0 && !(c == '<')) {
						System.out.print("<span class=\"xml-atn\">" + c);
						continue;
					}
					if (c == '<') {
						// 左标签
						TAG = 1;
						System.out.print("<span class=\"xml-tag\">&lt;");
					} else if (c == '>') {
						// 右标签
						TAG = 0;
						System.out.print("&gt;</span>");
					} else if (c == '"') {
						// 左双引号
						FLAG = 1;
						System.out.print("<span class=\"xml-atv\">" + c);
					} else if (c == ' ') {
						// 空格和属性
						System.out.print(" ");
						char next = trim.charAt(k + 1);
						if (!(next == '/')) {
							System.out.print("<span class=\"xml-atn\">");
						}
					} else if (c == '=') {
						// 等号
						System.out
								.print("</span><span class=\"xml-pun\">=</span>");
					} else {
						System.out.print(c);
					}
				}
			}
		}
		System.out.println("");
	}

}
